package com.tju.resource.server.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.resource.mapper.TbRawMatlMapper;
import com.tju.resource.model.TbRawMatl;
import com.tju.resource.server.TbRawMatlService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 */
@Service
public class TbRawMatlServiceImpl extends ServiceImpl<TbRawMatlMapper, TbRawMatl>
        implements TbRawMatlService {
    @Autowired
    private TbRawMatlMapper tbRawMatlMapper;
    @Autowired
    private ChineseExceptionUtils zh_CH;

    /**
     * 查询原材料
     */
    @Override
    public Result<Object> selectTbRawMatl(ClientParameters param) {
        if (EmptyUtils.isNotEmpty(param.getLimit()) && EmptyUtils.isNotEmpty(param.getPage())) {
            Page<TbRawMatl> page = new Page<>(param.getPage(), param.getLimit());
            QueryWrapper<TbRawMatl> qw = new QueryWrapper<>();
            qw.like("rm_no", param.getOne());
            qw.like("rm_name", param.getTwo());
            qw.orderByDesc("id");
            Page<TbRawMatl> tbRawMatlPage = tbRawMatlMapper.selectPage(page, qw);
            return ResultUtil.success(ResultEnum.SUCCESS, tbRawMatlPage.getRecords(), tbRawMatlPage.getTotal());
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.MissingParameters);
    }

    /**
     * 添加原材料
     */
    @Override
    @Transactional
    public Result<Object> insertTbRawMatl(TbRawMatl rawMatl) {
        QueryWrapper<TbRawMatl> qw = new QueryWrapper<>();
        qw.eq("rm_no", rawMatl.getRmNo());
        if (EmptyUtils.isNotEmpty(tbRawMatlMapper.selectOne(qw)))
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "原材料编号不能重复!");
        if (tbRawMatlMapper.insert(rawMatl) > 0) {
            return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.AddedSuccessfully);
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.AddFailed);
    }

    /**
     * 修改原材料
     */
    @Override
    @Transactional
    public Result<Object> updateTbRawMatl(TbRawMatl rawMatl) {
        QueryWrapper<TbRawMatl> qw = new QueryWrapper<>();
        qw.eq("rm_no", rawMatl.getRmNo());
        TbRawMatl tbRawMatl = tbRawMatlMapper.selectOne(qw);
        if (EmptyUtils.isNotEmpty(tbRawMatl)) {
            if (!tbRawMatl.getId().equals(rawMatl.getId()))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "原材料编号不能重复!");
        }
        if (EmptyUtils.isNotEmpty(rawMatl.getId())) {
            if (tbRawMatlMapper.updateById(rawMatl) > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyModified);
            }
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailToEdit);
    }

    /**
     * 删除原材料
     *
     * @param rawMatl
     * @return
     */
    @Override
    @Transactional
    public Result<Object> deleteTbRawMatl(TbRawMatl rawMatl) {
        if (EmptyUtils.isNotEmpty(tbRawMatlMapper.selectTbRawMatlById(rawMatl.getId(),null)))
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "该原料被使用了不能删除!");
        if (EmptyUtils.isNotEmpty(rawMatl.getId())) {
            if (tbRawMatlMapper.deleteById(rawMatl.getId()) > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyModified);
            }
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailedToDelete);
    }
}




